
import os
from config.PathConfig import *
LOG_DIR = os.path.join(ROOTDIR, "logs")

from datetime import datetime

class Log:
    def __init__(self, log_file="bot.log", log_dir= LOG_DIR):
        """初始化日志类，指定文件路径"""
        self.log_dir = log_dir
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)
        self.log_file = os.path.join(log_dir, log_file)
    
    def _write(self, level, message):
        """写入日志到文件"""
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        log_entry = f"[{timestamp}] [{level}] {message}\n"
        with open(self.log_file, "a", encoding="utf-8") as f:
            f.write(log_entry)
    
    def info(self, message):
        """记录 INFO 级别日志"""
        self._write("INFO", message)
    
    def error(self, message):
        """记录 ERROR 级别日志"""
        self._write("ERROR", message)
    
    def debug(self, message):
        """记录 DEBUG 级别日志"""
        self._write("DEBUG", message)
    
    def clear(self):
        """清空日志文件"""
        with open(self.log_file, "w", encoding="utf-8") as f:
            f.truncate(0)


logger = Log()
# # 示例用法
# if __name__ == "__main__":
#     logger = Log()
#     logger.info("机器人启动啦")
#     logger.error("哎呀，出错啦")
#     logger.debug("调试一下")